package java_0554_generic;

import java.util.ArrayList;
import java.util.Comparator;

public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> arrayList = new ArrayList<>();
        arrayList.add(new Employee("Elysia", 10000, new MyDate(2003, 3, 22)));
        arrayList.add(new Employee("Hua", 8000, new MyDate(2002, 2, 22)));
        arrayList.add(new Employee("Kiana", 9000, new MyDate(2003, 2, 22)));

        //1.name的比较采用外部方式，构建一个匿名内部类Comparator，
        // 将 Comparator 对象作为参数传递给arrayList.sort
        //2.MyDate直接实现Comparable<MyDate>，在类的内部写compareTo方法
        arrayList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (o1 == null && o2 == null) {
                    throw new NullPointerException("传入的是空值");
                }

                //比较name，采用String已经实现的compareTO
                int result = o1.getName().compareTo(o2.getName());
                if (result != 0) {
                    return result;
                }

                //比较birthday
                result = o1.getBirthday().compareTo(o2.getBirthday());
                return result;
            }
        });

        System.out.println(arrayList);
    }
}

class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\n" + "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

}

class MyDate implements Comparable<MyDate> {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "year=" + year +
                ", month=" + month +
                ", day=" + day;
    }

    @Override
    public int compareTo(MyDate o) {

        //比较年
        int yearMinus = this.getYear() - o.getYear();
        if (yearMinus != 0) {//如果年不相同则直接返回
            return yearMinus;
        }
        //如果年相同则继续比较月
        int monthMinus = month - o.getMonth();
        if (monthMinus != 0) {
            return monthMinus;
        }

        int dayMinus = day - o.getDay();
        if (dayMinus != 0) {
            return dayMinus;
        }
        //年月日都相同返回0
        return 0;

    }
}